﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSLib.CodeFormating.Formatters;
using System.Xml.Linq;
using PSLib.CodeFormating.Lexems;

namespace LuaCodeStyler
{
	public static class XMLOperations
	{
		private static void AddTextElement(this XElement element, string key, string value)
		{
			XElement el = new XElement(key);
			el.Value = value;
			element.Add(el);
		}

		private static string GetTextValue(this XElement element, string key)
		{
			try
			{
				return element.Element(key).Value.ToString();
			}
			catch (Exception)
			{

			}

			return string.Empty;
		}

		private static string GetAttributeValue(this XElement element, string key)
		{
			try
			{
				return element.Attribute(key).Value.ToString();
			}
			catch (Exception)
			{

			}

			return string.Empty;
		}

		public static void Export(string path, FormatConfiguration fc)
		{
			try
			{
				XElement root = new XElement("LuaCodeStyle");

				root.AddTextElement("name", fc.Name);
				root.AddTextElement("description", fc.Description);
				root.AddTextElement("prefix", fc.Prefix);
				root.AddTextElement("postfix", fc.Postfix);

				XElement lexemConfigurationCollection = new XElement("lexems");
				root.Add(lexemConfigurationCollection);
				foreach (var item in fc.LexemConfigurationCollection)
				{
					XElement lexem = new XElement("lexem");
					lexemConfigurationCollection.Add(lexem);

					lexem.Add(new XAttribute("type", item.LexemType.ToString()));
					lexem.AddTextElement("name", item.Name);
					lexem.AddTextElement("description", item.Description);
					lexem.AddTextElement("prefix", item.Prefix);
					lexem.AddTextElement("postfix", item.Postfix);

					XElement lexemReplacements = new XElement("replacements");
					lexem.Add(lexemReplacements);
					foreach (var rItem in item.ReplacementCollection)
					{
						XElement lexemReplacement = new XElement("replacement");
						lexemReplacement.Add(new XAttribute("source", rItem.Key), new XAttribute("destination", rItem.Value));
						lexemReplacements.Add(lexemReplacement);
					}
				}


				XDocument doc = new XDocument(root);
				doc.Save(path);
			}
			catch (Exception)
			{
			}
		}

		public static FormatConfiguration Import(string path)
		{
			try
			{
				FormatConfiguration fc = new FormatConfiguration();
				XDocument doc = XDocument.Load(path);
				
				XElement root = doc.Root;
				fc.Name = root.GetTextValue("name");
				fc.Description = root.GetTextValue("description");
				fc.Prefix = root.GetTextValue("prefix");
				fc.Postfix = root.GetTextValue("postfix");

				XElement lexems = root.Element("lexems");
				if (lexems != null)
				{
					foreach (XElement item in lexems.Elements())
					{
						LexemType type = LexemType.None;
						try
						{
							type = (LexemType)Enum.Parse(typeof(LexemType), item.Attribute("type").Value);
						}
						catch (Exception) {}
						if (type == LexemType.None) continue;

						LexemConfiguration lc = new LexemConfiguration(type);
						lc.Name = item.GetTextValue("name");
						lc.Description = item.GetTextValue("description");
						lc.Postfix = item.GetTextValue("postfix");
						lc.Prefix = item.GetTextValue("prefix");

						XElement replacement = item.Element("replacements");
						if (replacement != null)
						{
							foreach (XElement rItem in replacement.Elements())
							{
								string key = rItem.GetAttributeValue("source");
								string value = rItem.GetAttributeValue("destination");
								lc.ReplacementCollection.Add(key,value);
							}
						}
						fc.LexemConfigurationCollection.Add(lc);
					}
				}
				return fc;
			}
			catch (Exception)
			{
			}
		
			return null;
		}
	}
}
